/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import java.sql.*;
import Module.*;

/**
 *
 * @author Vegard
 */
public class ChattLogController {

     private Database database = new Database();

    public boolean registrerChattLog(ChattLog chattLog){

        Connection conn = null;
        try{
            conn = DriverManager.getConnection(database.getURL());
            String[] autogen = {"id"};
            PreparedStatement statement = conn.prepareStatement(
                    "Insert into chattLog values(DEFAULT,?,?)",autogen);
            statement.setInt(1, chattLog.getSessionId() );
            statement.setString(2, chattLog.getChattLog());
            statement.executeUpdate();
            conn.close();
            return true;
        }catch (Exception e) {
            System.err.println(e);

        }
        return false;
    }
    public boolean editChattLog(ChattLog chattLog){
        Connection conn = null;
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("Update chattLog "
                    + "set sessionId=?, logg=? where chattID =?");
            statement.setInt(1, chattLog.getSessionId());
            statement.setString(2, chattLog.getChattLog());
            statement.setInt(3, chattLog.getId());
            statement.executeUpdate();
            conn.close();
            return true;
        }catch(Exception e){
            System.err.println(e);
        }

        return false;
        }

    public boolean deleteChattLog(ChattLog chattLog){
        Connection conn = null;
        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("delete from chattLog where chattID =?");
            statement.setInt(1, chattLog.getId());
            statement.executeUpdate();
            conn.close();
            return true;
        }catch(Exception e){
            System.err.println(e);
        }
        return false;
    }
    public ChattLog getChattLogToSession(Session session){
        Connection conn = null;

        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("select * from chattlog where sessionId=?");
            statement.setInt(1,session.getId());
            ResultSet rs = statement.executeQuery();
            while(rs.next()){
                ChattLog cl = new ChattLog(rs.getInt(1),rs.getInt(2),rs.getString(3));
                conn.close();
                return cl;

            }

        }catch(Exception e){
            System.err.println(e);
        }
        return null;
    }
    public ChattLog getChattLog(ChattLog chattlog){
        Connection conn = null;

        try{
            conn = DriverManager.getConnection(database.getURL());
            PreparedStatement statement = conn.prepareStatement("select * from chattlog where chattLogId=?");
            statement.setInt(1,chattlog.getId());
            ResultSet rs = statement.executeQuery();
            while(rs.next()){
                conn.close();
                return new ChattLog(rs.getInt(1),rs.getInt(2),rs.getString(3));
            }

        }catch(Exception e){
            System.err.println(e);
        }
        return null;
    }
}
